﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using jnxceq.Common.Net;

namespace jnxceq
{
    public delegate void DdiplayData(string data);
    public delegate void DdisplayConn(string conn);
    public delegate void DdisplayEqu(string typeid);
    public partial class Form1 : Form
    {
        public static DdiplayData DdisData = null;
        public static DdisplayConn DdisConn = null;
        public static DdisplayEqu DdisEqu=null;
        public static Dictionary<string, String> dict = new Dictionary<string, string>(); 
        public Form1()
        {
            InitializeComponent();

            DdisData = DisplayData;
            DdisConn = DisplayConnecttion;
            DdisEqu = DisplayEqu;
            
        }

        
        private void button1_Click(object sender, EventArgs e)
        {
            Connetion con = new Connetion();
            con.Start();
        }
        private void DisplayEqu(string typeid)
        {
            if (this.lvequ.InvokeRequired)
            {
                this.lvequ.BeginInvoke(new DdisplayEqu(DdisEqu), typeid);

            }
            else
            {
                bool isfind = false;
                string[] s = typeid.Split('&');
                this.lvequ.BeginUpdate();   //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度
                foreach (ListViewItem item in this.lvequ.Items)
                {

                    if (item.SubItems[1].Text == s[1])
                    {
                        isfind = true;
                        item.SubItems[2].Text = s[2];
                        break;
                    }
                    
                }
                if (!isfind)
                {

                    ListViewItem lvi = new ListViewItem();
                    lvi.ImageIndex = 0;    //通过与imageList绑定，显示imageList中第i项图标

                    lvi.Text = s[0];
                    lvi.SubItems.Add(s[1]);
                    lvi.SubItems.Add(s[2]);
                    lvi.SubItems.Add(s[3]);
                    lvi.SubItems.Add("");
                    dict.Add(s[1], "");
                    this.lvequ.Items.Add(lvi);
                }
                this.lvequ.EndUpdate();  
            }
 

        }
        private void DisplayData(string data)
        {
            if (this.txt_data.InvokeRequired)
            {
                this.txt_data.BeginInvoke(new DdiplayData(DdisData), data);

            }
            else
            {                
                string str = data + "\r\n";                
                this.txt_data.AppendText(str);
            }
        
        }

        private void DisplayConnecttion(string conn)
        {
            if (this.txt_connect.InvokeRequired)
            {
                this.txt_data.BeginInvoke(new DdisplayConn(DisplayConnecttion), conn);
            }
            else
            {
                string str = conn + "\r\n";
                this.txt_connect.AppendText(str);
            }
        }
        
        private void txt_connect_TextChanged(object sender, EventArgs e)
        {
            this.txt_connect.ScrollToCaret();
        }

        private void txt_data_TextChanged(object sender, EventArgs e)
        {
            this.txt_data.ScrollToCaret();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //ColumnHeader ch = new ColumnHeader();

            //ch.Text = "列标题1";   //设置列标题

            //ch.Width = 120;    //设置列宽度

            //ch.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式

            //this.listView1.Columns.Add(ch);    //将列头添加到ListView控件。
        }

        private void lvequ_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && lvequ.SelectedItems.Count == 1)
            {
                contextMenuStrip1.Show(MousePosition);
            }
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem ss = sender as ToolStripMenuItem;
            if (ss != null)
            {
                if (lvequ.SelectedItems.Count == 1)
                {
                    string r= ss.Text;
                    lvequ.SelectedItems[0].SubItems[4].Text =r;
                    string s = "";
                    if (r == "10分钟")
                        s = "00";
                    else if ( r== "30分钟")
                       s = "01";
                    else if (r == "1小时")
                        s = "02";
                    else if (r == "2小时")
                        s = "03";
                    else if (r == "4小时")
                       s = "04" ;
                    else if (r == "6小时")
                        s = "05";
                    else if ( r == "12小时")
                      s = "06" ;
                    else if (r == "24小时")
                       s = "07" ;

                    string id = lvequ.SelectedItems[0].SubItems[1].Text;
                    if (dict.ContainsKey(id) == false)
                    {
                        dict.Add(id, s);
                    }
                    else
                    {
                        dict[id] = s;
                    }

                }
            }
        }
    }
}
